jQuery.fn.initAddressSelect = function(){
	$(this).empty()
	$(this).append("<option value=''>请选择</option>")
}

function showAddAddressDialog() {
    var form = $("#new-address-form")
	$("#add-address-dialog").dialog({
		height:360,
		width :580,
		title :"新增收货地址",
		buttons:{  
                "确定":function(){  
                    $.ajax({  
                        url:form.attr('action'),  
                        type:form.attr('method'),  
                        data:form.serialize(),  
                        dataType:"json",  
                        success : function(data) {
							if (data.code) {
								alert(data.msg)
								$("#add-address-dialog").dialog("close")
							} else {
								alert(data.msg)
								$("#add-address-dialog").dialog("close")

							}
						},
                        error:function(){  
                            $("#add-address-dialog").dialog("close");  
                            alert("出错了哦");  
                        } 
                    })
                },  
                "关闭": function() {  
                    $("#add-address-dialog").dialog("close");  
                }  
        	}  
	})
}

function showEditAddressDialog(id,obj) {

	var info       = $(obj).closest(".address-box")
	var form       = $("#edit-address-form")
	var selectBox  = $("#address-choic-edit")
	var aeraDiv    = info.find(".ib").eq('1')

	var provinceSelect = selectBox.children(".select").eq('0')
	var citySelect     = selectBox.children(".select").eq('1')
	var countySelect   = selectBox.children(".select").eq('2')

	var consignee  = info.find(".ib").eq('0').html()
	var provinceId = aeraDiv.children('span').eq('0').attr('area-id')
	var cityId     = aeraDiv.children('span').eq('1').attr('area-id')
	var countyId   = aeraDiv.children('span').eq('2').attr('area-id')
	var address    = info.find(".ib").eq('2').html()
	var mobile     = info.find(".ib").eq('3').html()

	var url   = $("#item-address").attr('get-all-url')
	var param = 'id%5B%5D=0&id%5B%5D=' + provinceId + '&id%5B%5D=' + cityId 

	$.ajax({
		url: url,
		type : "POST",
		data : param,
		dataType : "json",
		success : function(data) {
			if (data.code) {
				var msg = data.msg
				$.each(msg, function(index, data) {
					var data2 = data
					var select 
					var value
					switch (index) {
						case 0:
							select = provinceSelect
							value  = provinceId
							break
						case 1:
							select = citySelect
							value  = cityId
							break
						case 2:
							select = countySelect
							value  = countyId
							break	
					}
					select.initAddressSelect()
					$.each(data, function(index2, data2) {
						select.append('<option value="' + data2.id + '">' + data2.areaname + '</option>') 
					})
					select.val(value)
				})
			} else {
				alert('data.msg')
			}
		}
	})

	form.children('input').eq('0').val(id)
	form.children('input').eq('1').val(consignee)
	form.children('input').eq('2').val(mobile)
	form.children('input').eq('3').val(address)

	$("#edit-address-dialog").dialog({
		height:360,
		width :580,
		title :"修改收货地址",
		buttons:{  
                "确定":function(){
                    $.ajax({  
                        url:form.attr('action'),  
                        type:form.attr('method'),  
                        data:form.serialize(),  
                        dataType:"json",  
                        success : function(data) {
							if (data.code) {
								alert(data.msg)
								$("#edit-address-dialog").dialog("close")
							} else {
								alert(data.msg)
								$("#edit-address-dialog").dialog("close")

							}
						},
                        error:function(){  
                            $("#edit-address-dialog").dialog("close");  
                            alert("出错了哦");  
                        } 
                    })
                },  
                "关闭": function() {  
                    $("#edit-address-dialog").dialog("close");  
                }  
        	}  
	})
}

function setAddressDefault(id) {
	var url = $("#item-address").attr("default-url")
	var param = {"id" : id}
	$.ajax({
		url  : url,
		type : "POST",
		data : param,
		success : function(data){
			if (data.code) {
				alert(data.msg)
			} else {
				alert(data.code)
			}
		},
		error : function(){
			alert("参数错误")
		}
	})
}

function deleteAddress(id) {
	var url = $("#item-address").attr("delete-url")
	var param = {"id" : id}
	$.ajax({
		url  : url,
		type : "POST",
		data : param,
		success : function(data){
			if (data.code) {
				alert(data.msg)
			} else {
				alert(data.code)
			}
		},
		error : function(){
			alert("参数错误")
		}
	})
}

$(document).ready(function(){
	var getAreaUrl = $("#item-address").attr("get-url")

	//初始化省市列表
	function initProvinceSelect() {
		$.ajax({
			type : "POST",
			url : getAreaUrl,
			data : "id=0",
			success : function (data) {
				var msg = data.msg
				$.each(msg, function(index, data) {
					$(".province").append('<option value="' + data.id + '">' + data.areaname + '</option>')
				})
			},
			error : function () {
				alert("页面错误")
			}
		})
	}

	//获取城市列表
	$('.province').change(function() {
		var areaId = $(this).val()
		if (areaId != '') {
			var select = $(this)
			var city = select.siblings(".city")
			var county = select.siblings(".county")
			city.initAddressSelect()
			county.initAddressSelect()
			$.ajax({
				cache : true,
				type : "POST",
				url : getAreaUrl,
				data : "id="+areaId,
				success : function (data) {
					var msg = data.msg
					$.each(msg, function(index, data) {
						city.append('<option value="' + data.id + '">' + data.areaname + '</option>')
					})
				},
				error : function () {
					alert("页面错误")
				}
			})
		} else {
			select.siblings(".city").initAddressSelect()
			select.siblings(".county").initAddressSelect()
		}
	})

	$('.city').change(function() {
		var areaId = $(this).val()
		if (areaId != '') {
			var select = $(this)
			var county = select.siblings(".county")
			county.initAddressSelect()
			$.ajax({
				cache : true,
				type : "POST",
				url : getAreaUrl,
				data : "id="+areaId,
				success : function (data) {
					var msg = data.msg

					$.each(msg, function(index, data) {
						county.append('<option value="' + data.id + '">' + data.areaname + '</option>')
					})
				},
				error : function () {
					alert("页面错误")
				}
			})
		} else {
			select.siblings(".county").initAddressSelect()
		}
	})

	initProvinceSelect()

})